﻿// // ******************************************************************
// //       /\ /|       @file       BaseService.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-31 20:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-11-07 19:11 Await
// // ******************************************************************

using System.Security.Authentication;

namespace AutomaticSubtitles.Service.Services.Base;

public abstract class BaseService<T> : ServiceBase where T : class
{
    // protected const string AddMsg = "添加失败";
    // protected const string DeleteMsg = "删除失败";
    // protected const string UpdateMsg = "修改失败";
    // protected const string QueryMsg = "查询成功";

    protected ILogger<T> _logger => GetRequiredService<ILogger<T>>();

    protected IEventBus _eventBus => GetRequiredService<IEventBus>();

    protected string GetUserId()
    {
        var userContext = GetRequiredService<IUserContext>();
        var userId = userContext.GetUserId<string>();
        return userId ?? throw new AuthenticationException("用户未登录");
    }

    protected async Task PublishAsync<TEvent>(TEvent @event) where TEvent : IEvent
    {
        await _eventBus.PublishAsync(@event);
    }
}